package com.jmu.edu.IMApplication.adapter;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.imageview.ShapeableImageView;
import com.jmu.edu.IMApplication.MyApplication;
import com.jmu.edu.IMApplication.R;
import com.jmu.edu.IMApplication.event.SwitchFragmentEvent;
import com.jmu.edu.IMApplication.fragment.AddFriendFragment;
import com.jmu.edu.IMApplication.fragment.ChatFragment;
import com.jmu.edu.IMApplication.fragment.ShowUserFragment;
import com.jmu.edu.IMApplication.utils.MsgUtils;
import com.jmu.edu.IMApplication.utils.ViewUtils;
import com.jmu.edu.IMApplication.utils.WillowUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.jpush.im.android.api.callback.GetAvatarBitmapCallback;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.UserInfo;

public class SearchFriendItemAdapter extends RecyclerView.Adapter<SearchFriendItemAdapter.MyViewHolder> {


    private List<UserInfo> userInfoList;

    public void setUserInfoList(List<UserInfo> userInfoList) {
        this.userInfoList = userInfoList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_contact, parent, false);
        view.setBackgroundResource(R.drawable.rounded_background_pinkblue);
        return new MyViewHolder(view);
    }


    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        UserInfo userInfo = userInfoList.get(position);
        holder.nicknameText.setText(WillowUtils.getNickName(userInfo));
        holder.signText.setText(WillowUtils.getSignature(userInfo));
        // 加载avatar
        userInfo.getAvatarBitmap(new GetAvatarBitmapCallback() {
            @Override
            public void gotResult(int i, String s, Bitmap bitmap) {
                if (bitmap != null) {
                    Glide.with(holder.avatarImage).load(bitmap).into(holder.avatarImage);
                } else {
                    Glide.with(holder.avatarImage).load(R.drawable.smc).into(holder.avatarImage);
                }
            }
        });
        //根据用户身份来展示不一样的界面
        if (userInfo.getUserID() == MyApplication.getUser().getUserID()) {
            holder.stateText.setVisibility(View.VISIBLE);
            holder.addFriendButton.setVisibility(View.GONE);
            holder.stateText.setText("本用户");
        } else if (userInfo.isFriend()) {
            holder.stateText.setVisibility(View.VISIBLE);
            holder.addFriendButton.setVisibility(View.GONE);
            holder.stateText.setText("已添加为好友");
        } else {
            holder.addFriendButton.setVisibility(View.VISIBLE);
            holder.stateText.setVisibility(View.GONE);
        }


        //点击添加好友按钮会跳转到添加好友的详细界面
        holder.addFriendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "addFriend";
                AddFriendFragment addFriendFragment = (AddFriendFragment) MyApplication.fragmentManager.findFragmentByTag(tag);
                if (addFriendFragment == null) {
                    addFriendFragment = new AddFriendFragment();
                }
                addFriendFragment.setUserInfo(userInfo);
                //发送事件给Fragment进行处理
                EventBus.getDefault().post(new SwitchFragmentEvent(addFriendFragment, tag, "searchFriend"));
            }
        });
        //点击这个item会跳到用户个人展示界面
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String tag = "showUser";
                ShowUserFragment show = (ShowUserFragment) MyApplication.fragmentManager.findFragmentByTag(tag);
                if (show == null) {
                    show = new ShowUserFragment();
                }
                show.setUserInfo(userInfo);
                //发送事件给Fragment进行处理
                EventBus.getDefault().post(new SwitchFragmentEvent(show, tag, "searchFriend"));

            }
        });

    }

    @Override
    public int getItemCount() {
        Log.d("好友列表适配器", "当前好友列表数量:" + userInfoList.size());
        return userInfoList.size();
    }


    public static class MyViewHolder extends RecyclerView.ViewHolder {

        private ShapeableImageView avatarImage;
        private TextView nicknameText, signText, stateText;
        private MaterialButton addFriendButton;

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            avatarImage = itemView.findViewById(R.id.item_contact_avatar);
            nicknameText = itemView.findViewById(R.id.item_contact_nickname);
            signText = itemView.findViewById(R.id.item_contact_sign);
            addFriendButton = itemView.findViewById(R.id.item_contact_add_friend);
            stateText = itemView.findViewById(R.id.item_contact_state);
        }
    }
}
